unit PPS_Edit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ZAbstractTable, ZDataset, DB, ZAbstractRODataset, ZAbstractDataset,
  ZConnection, StdCtrls, Setting;

type
  TPPS_Edit_Form = class(TForm)
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    namainput: TEdit;
    jabatancombo: TComboBox;
    tambahbtn: TButton;
    batalbtn: TButton;
    ZConnection1: TZConnection;
    ZQuery1: TZQuery;
    ZTable1: TZTable;
    textkota: TEdit;
    textkec: TEdit;
    textdesa: TEdit;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure batalbtnClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure tambahbtnClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    constructor Create(AOwner: TComponent; posisi, kota, kec, desa, nama,
      jabatan, id: string); overload;
  end;

var
  PPS_Edit_Form: TPPS_Edit_Form;
  iposisi, ikota, ikec, idesa, inama, ijab, iid: string;

implementation

{$R *.dfm}

constructor TPPS_Edit_Form.Create(AOwner: TComponent; posisi, kota, kec, desa,
  nama, jabatan, id: string);
begin
  iposisi := posisi;
  ikota := kota;
  ikec := kec;
  idesa := desa;
  inama := nama;
  ijab := jabatan;
  iid := id;
  inherited Create(AOwner);
end;

procedure TPPS_Edit_Form.batalbtnClick(Sender: TObject);
begin
  Close;
end;

procedure TPPS_Edit_Form.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TPPS_Edit_Form.FormCreate(Sender: TObject);
var setting:TSetting_Form;
begin
  setting:=TSetting_Form.Create;
  ZConnection1.HostName:=setting.host;
  ZConnection1.Protocol:=setting.protocol;
  ZConnection1.Database:=setting.database;
  ZConnection1.User:=setting.user;
  ZConnection1.Password:=setting.password;
  ZConnection1.Connected:=true;
  ztable1.Connection:=ZConnection1;
  ZQuery1.Connection:=ZConnection1;
  ZConnection1.LoginPrompt:=False;
  ZConnection1.Connected := True;
  BorderIcons := BorderIcons - [biMaximize];

    textkota.Text := ikota;
  textkec.Text := ikec;
  textdesa.Text := idesa;
  namainput.Text := inama;
  jabatancombo.Items.Add('Ketua');
  jabatancombo.Items.Add('Anggota');
  jabatancombo.ItemIndex:=0;
  if(ijab='Ketua')then
  begin
    jabatancombo.ItemIndex:=0;
  end
  else
    jabatancombo.ItemIndex:=1;
end;

procedure TPPS_Edit_Form.tambahbtnClick(Sender: TObject);
var kodekabkot,kodekec,kodedes:string;
begin
  with ztable1 do
  begin
    active:=false;
    tablename:='petugas';
    filter:='ID_PETUGAS='+QuotedStr(iid);
    filtered:=true;
    active:=true;
    first;
    kodekabkot:=FieldByName('KODE_KABKOT').AsString;
    kodekec:=FieldByName('KODE_KECAMATAN').AsString;
    kodedes:=FieldByName('KODE_DESA').AsString;
  end;

  if(jabatancombo.ItemIndex=0)then
  begin
    with ztable1 do
    begin
      active:=false;
      tablename:='petugas';
      filter:='ID_PETUGAS<>'+QuotedStr(iid)+' and KODE_DESA='+QuotedStr(kodedes)+' and KODE_KECAMATAN='+QuotedStr(kodekec)+' and KODE_KABKOT='+QuotedStr(kodekabkot)+' and JABATAN='+QuotedStr('1');
      filtered:=true;
      active:=true;
      first;
      if(RecordCount>=1)then
      begin
        ShowMessage('Jabatan ketua sudah dipegang orang lain');
      end
      else
      begin
        with zquery1 do
        begin
          active:=false;
          sql.Clear;
          sql.Text:='update petugas set NAMA='+QuotedStr(namainput.Text)+', JABATAN='+QuotedStr(inttostr(jabatancombo.ItemIndex+1))+' where ID_PETUGAS='+QuotedStr(iid);
          ExecSQL;
        end;
        ShowMessage('Data telah berhasil diubah');
      end;
    end;
  end
  else
    begin
      with ztable1 do
      begin
        active:=false;
        tablename:='petugas';
        filter:='ID_PETUGAS<>'+QuotedStr(iid)+' and KODE_DESA='+QuotedStr(kodedes)+' and KODE_KECAMATAN='+QuotedStr(kodekec)+' and KODE_KABKOT='+QuotedStr(kodekabkot)+' and JABATAN='+QuotedStr('2');
        showmessage('ID_PETUGAS<>'+QuotedStr(iid)+' and KODE_DESA='+QuotedStr(kodedes)+' and KODE_KECAMATAN='+QuotedStr(kodekec)+' and KODE_KABKOT='+QuotedStr(kodekabkot)+' and JABATAN='+QuotedStr('2'));
        filtered:=true;
        active:=true;
        first;

        if((kodedes<>'') AND (RecordCount>=2))then
        begin
          ShowMessage('Jabatan anggota sudah berjumlah 2 orang');
        end
        else if(RecordCount>=4) then begin
          ShowMessage('Jabatan anggota sudah berjumlah 4 orang');
        end
        else
        begin
          with zquery1 do
          begin
            active:=false;
            sql.Clear;
            sql.Text:='update petugas set NAMA='+QuotedStr(namainput.Text)+', JABATAN='+QuotedStr(inttostr(jabatancombo.ItemIndex+1))+' where ID_PETUGAS='+QuotedStr(iid);
            ExecSQL;
          end;
          ShowMessage('Data telah berhasil diubah');
        end;
      end;

    end;


end;

end.
